﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Collections;
using Microsoft.Xna.Framework;

namespace Pavilion.DesignerTool.ViewTools
{
    public abstract class PropertyItemBase
    {
        public abstract string PropertyName { get; set; }
        public abstract object PropertyValue { get; set; }
        public abstract object AssociatedItem { get; set; }
        public abstract System.Windows.FrameworkElement FrameworkElement { get; set; }

        protected virtual void LoadDefaultFrameworkElement()
        {
            if (PropertyValue is bool)
                LoadCheckBox();
            else if (PropertyValue is Enum)
                LoadComboBox(this, PropertyValue, Enum.GetValues(PropertyValue.GetType()), null);
            else
                LoadTextBox();
        }

        private void LoadTextBox()
        {
            if (FrameworkElement is TextBox)
                return;

            TextBox textBox = new TextBox();
            FrameworkElement = textBox;

            textBox.Text = GameEngine.GetStringRepresention(PropertyValue);
            textBox.LostFocus += new RoutedEventHandler(TextBox_LostFocus);
        }

        private void LoadCheckBox()
        {
            if (FrameworkElement is CheckBox)
                return;

            CheckBox checkBox = new CheckBox();
            FrameworkElement = checkBox;

            checkBox.IsChecked = (bool)PropertyValue;
            checkBox.VerticalAlignment = VerticalAlignment.Center;
            checkBox.SetBinding(CheckBox.IsCheckedProperty, CreateBinding(this));
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;

            try
            {
                PropertyValue = GameEngine.ParseObject((PropertyValue == null) ? typeof(string) : PropertyValue.GetType(), textBox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                textBox.Text = GameEngine.GetStringRepresention(PropertyValue);
            }
        }

        static public void LoadComboBox(PropertyItemBase propertyItem, object propertyValue, IEnumerable itemsSource, SelectionChangedEventHandler selectionChangedHandler)
        {
            if (propertyItem.FrameworkElement is ComboBox)
                return;

            ComboBox comboBox = new ComboBox();
            propertyItem.FrameworkElement = comboBox;

            comboBox.Style = (Style)comboBox.FindResource(ToolBar.ComboBoxStyleKey);
            comboBox.ItemsSource = itemsSource;
            comboBox.SelectedItem = propertyValue;
            comboBox.HorizontalAlignment = HorizontalAlignment.Stretch;

            if (selectionChangedHandler != null)
                comboBox.SelectionChanged += selectionChangedHandler;
            else
                comboBox.SetBinding(ComboBox.SelectedItemProperty, CreateBinding(propertyItem));
        }

        static private Binding CreateBinding(PropertyItemBase propertyItem)
        {
            Binding binding = new Binding();
            binding.Source = propertyItem;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            binding.Path = new PropertyPath("PropertyValue");

            return binding;
        }
    }
}
